<?php
/**
 * Created by PhpStorm.
 * User: 郑合达
 * Date: 2019/1/23
 * Time: 14:42
 */

namespace app\admin\controller;


use app\model\DeviceInfoModel;
use app\model\DeviceModel;
use app\model\MenuModel;
use think\Controller;
use app\model\GroupingModel;
use app\model\DtdataModel;
use app\model\UserModel;
use app\model\DandataModel;
use think\Cookie;
use think\Db;
use app\model\AppUserModel;
use app\model\EquipmentModel;
class Map extends Common
{
    //实验cookie
    public function cs_cookie()
    {

        if (!Cookie::get('one')){
            dump('wu');
            setcookie('one','ok01');
        }else{
            dump('1');
            dump(Cookie::get('one'));
        }


    }
    //定位监控页面
    public function map(){
        //获取该用户登陆的信息,存入session
       if(input('user_name')) {
          $user =AppUserModel::where('phone',input('user_name'))->find();
          session('admin',$user);
       }else if(session('admin')&&!input('user_name')){
           session('admin',session('admin'));
       } else {
           session('admin',session('user')); //最开始登陆的用户信息
    }
        $mid = MenuModel::where('user_id',session('admin.id'))->select()->toArray();
        $men= MenuModel::select()->toArray();

        $uid =$this->shu3($men,$mid,[]); //获取登陆管理者的全部客户id
        if($uid){
            $user_id=$uid;
        }else{
            $user_id=session('admin.id');
        }
//         //是否在线
        $online=input('online');
        //判断是否在线
        if($online){
            if($online==1){
                $where['d.online']=1;
            }elseif ($online==2){
                $where['d.online']=0;
            }
        }else{
            $online=0;
            $where=[];
        }
        $this->assign('online',$online);

        $imei = EquipmentModel::where('equ_user','in',$user_id)->column('name');
        session('imei',$imei);
        $info =EquipmentModel::alias('e')->field('e.name,e.equ_user,e.gr_id,e.nickname')
            ->field('d.*')
            ->join('gps_device_info d','e.name = d.imei')
            ->where('e.equ_user','in',$user_id)
            ->where($where)
            ->select();

      //是否在线统计
        $zoshu=0;
        $zshu =0;
        $bshu =0;
        if($imei){
            $zshu =DeviceInfoModel::where('imei','in',$imei)->where('online',1)->count(); //在线数量
            $zoshu = count($imei); //总数量
            $bshu =$zoshu -$zshu;//不在线设备数量

        }
        $this->assign('zoshu',$zoshu);
        $this->assign('zshu',$zshu);
        $this->assign('bshu',$bshu);
        $this->assign('info',$info);
        //设备在线判断

        return view('index');
    }
    /**
     * @return \think\response\Json
     * 地图数据返回
     */
    public function jwddata(){
        $where=['e.equ_user'=>['<>',0]];
        if (session('admin.id')!=4){
            $where['e.equ_user']=session('admin.id');
        }

 //判断是否在线
        $online =input('online');
        if($online){
            if($online==1){
                $where['d.online']=1;
            }elseif ($online==2){
                $where['d.online']=0;
            }
        }
		
        $data =EquipmentModel::alias('e')->field('e.name,e.equ_user,e.gr_id,e.nickname')
            ->field('d.*')
            ->join('gps_device_info d','e.name = d.imei')
            ->where($where)
            ->select();
//        dump($data->toArray());die();
        foreach ($data as $k=>$v){
            $time1=$v['updateTime']/1000;
            $data[$k]['updateTime']=date('Y-m-d H:i',$time1);
            $time=$v['lastLocationsTime']/1000;
            $data[$k]['lastLocationsTime']=date('Y-m-d H:i',$time);
        }
        if($data){
            return retJson($data,200,'成功');
        }
        return retJson($data,201,'失败');
    }



    //添加分组
    public function addgrouping(){
      $gr_name= input('post.');
      $gr_name['user_id']=session('user.id');
       $res = GroupingModel::create($gr_name);
       if($res){
           return retJson('',200,'添加成功');
       }
       return retJson('',201,'添加失败');
    }



   //电子栅栏
    public function index2(){
        return view('index2');
    }
    /*
     * 组名编辑
     */
    public function edit_zu()
    {
        $getData=input('post.');
      $res=  Db::table('gps_grouping')->update($getData);
        if($res){
            return retJson('',200,'编辑成功');
        }
        return retJson('',201,'编辑失败');
    }
 //路劲回放
    public function route(){
        $imei =input('imei');
//        dump($imei);die();
        $this->assign('imei',$imei);
        return view('route');
    }
    public function routef(){
        $imei =input('imei');
        $time=input('time');
        if($time){
            $startTime = strtotime($time)*1000;
            $endTime=$startTime+60*60*24*1000;
        }else{
            $startTime =strtotime(date("Y-m-d"),time())*1000;
            $endTime=$startTime+60*60*24;
        }
        $data= $this->curl("http://api.huixun.me/open/device-path?token=4d9262b1a5da42779110baa9df648743&deviceImei=$imei&type=&startTime=$startTime&endTime=$endTime");
        $data=json_decode($data,true);

        if ($data['code']==200){
            foreach ($data['data'] as $k=>$v){
               $bd_en= bd_encrypt($v['longitude'],$v['latitude']);//高德经纬度==转换==》百度经纬度
                $time=$v['createTime']/1000;
                $data['data'][$k]['createTime']=date('Y-m-d H:i',$time);
                $data['data'][$k]['longitude']=$bd_en['bd_lon'];
                $data['data'][$k]['latitude']=$bd_en['bd_lat'];
            }
        }

        if($data){
            return retJson($data,200,'成功');
        }
        return retJson($data,201,'失败');
    }
    //历史轨迹下载
    public function trajectory(){

        $header = ['序号','设备名称','设备号（ID）','设备手机卡号','创建时间','激活时间','到期时间'];
        //用于遍历的二维数组
        $result_data=array();
        for($i=0;$i<count($data);$i++){
            $new = array();
            $new[] =  $data[$i]['id'];
            $new[] =  $data[$i]['de_name'];
            $new[] =  $data[$i]['Devid'];
            $new[] =  $data[$i]['de_phone'];
            $new[] =  $data[$i]['creation_time'];
            $new[] =  $data[$i]['activation_time'];
            $new[] =  $data[$i]['due_time'];
            array_push($result_data,$new);
        }
        //$filename 文件名
        $filename = "设备列表".date("Y/m/d");
//        ExcelModel::getExcelFile(NULL,$header,$result_data,$filename,30);
        export($result_data, $header, $filename);
    }
    /**
     * @param $url   接口连接的地址
     * @return mixed
     *  转接接口的方法
     */
    public function curl($url){
        $ch = curl_init();
        //设置选项，包括URL
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        //执行并获取HTML文档内容
        $output = curl_exec($ch);
        //释放curl句柄
        curl_close($ch);
        $data=json_decode($output,true);//$data就是最终的数据 而且已经转换成了数组
        return $output;
    }
    /* 毫秒时间戳转换成日期 */
    public function msecdate($time)
    {
        $tag='Y-m-d H:i:s';
        $a = substr($time,0,10);
        $b = substr($time,10);
        $date = date($tag,$a).'.'.$b;
        return $date;
    }
}